package com.tanhua.houtai.controller;


import cn.hutool.core.util.StrUtil;
import com.tanhua.common.utils.NoAuthorization;
import com.tanhua.houtai.service.AdminService;
import com.tanhua.houtai.utils.RandomValidateCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.server.PathParam;
import java.time.Duration;
import java.util.Map;

@RestController
@RequestMapping("system")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;


    /**
     * 用户登录验证码图片
     *
     * @param
     * @return
     */
    @GetMapping("users/verification")
    @NoAuthorization
//    public void code(@PathParam("uuid") String uuid, HttpServletRequest request, HttpServletResponse response) {
//
//        response.setContentType("image/jpeg");//设置相应类型,告诉浏览器输出的内容为图片
//        response.setHeader("Pragma", "No-cache");//设置响应头信息，告诉浏览器不要缓存此内容
//        response.setHeader("Cache-Control", "no-cache");
//        response.setDateHeader("Expire", 0);
//        RandomValidateCodeUtil randomValidateCode = new RandomValidateCodeUtil();
//        String code = randomValidateCode.getRandcode(request, response);//输出验证码图片方法
//
//        String redisKey = "picture_code_" + uuid;
//        if (StrUtil.isNotEmpty(code)) {
//            this.redisTemplate.opsForValue().set(redisKey, code, Duration.ofMinutes(1));
//        }
//
//    }

    /**
     * 用户登录
     *
     * @param param
     * @return
     */
    @PostMapping("users/login")
    @NoAuthorization
    public ResponseEntity<Object> login(@RequestBody Map<String, String> param) {

                try {
                    String username = param.get("username");
                    String password = param.get("password");
                    String code = param.get("verificationCode");
                    String uuid = param.get("uuid");
                    //登录成功
                    Map<String, String> result = this.adminService.login(username, password, code,uuid);
                    return ResponseEntity.ok(result);
                } catch (Exception e) {
                    e.printStackTrace();
                }

        return null;
    }


    /**
     * 用户登录成功 进入主界面
     *
     * @param token
     * @return
     */

    @PostMapping("users/profile")
    @NoAuthorization
    public ResponseEntity<Object> profile(@RequestHeader("Authorization") String token) {
        try {
            //登录成功
            Map<String, Object> result = this.adminService.profile(token);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    /**
     * 用户退出
     *
     * @param
     * @return
     */
    @PostMapping("users/logout")
    @NoAuthorization
    public void logout(HttpServletRequest request) {
        try {
          request.getSession().invalidate();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
